﻿using System.Linq;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using ASPItalia.ModelVirtualCasting.WCF.Contracts;

namespace ASPItalia.ModelVirtualCasting.WCF.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ModelsManager : IModelsManager
    {
        #region IModelsManager Members

        [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
        public GetAllCandidatesResponse GetAllCandidates(GetAllCandidatesRequest request)
        {
            GetAllCandidatesResponse response = new GetAllCandidatesResponse();
            response.Models = (from m in ApplicationContext.Current.Candidates.Include("Multimedia").GetAll().ToArray()
                               select GetModel(m)).ToArray();

            return response;
        }

        [WebGet()]
        //[AspNetCacheProfile("CacheOneMinute")]
        public GetAllModelsResponse GetAllModels(GetAllModelsRequest request)
        {
            //throw new WebFaultException<ErrorDetails>(new ErrorDetails
            //{
            //    Message = "Accesso negato"
            //}, System.Net.HttpStatusCode.Forbidden);

            //Thread.Sleep(3000);

            GetAllModelsResponse response = new GetAllModelsResponse();
            response.Models = (from m in ApplicationContext.Current.Models.Include("Multimedia").GetAll().ToArray()
                               select GetModel(m)).ToArray();

            //response.Models = new Model[]
            //      {
            //          new Model { FirstName = "Anna", LastName = "Magnani",
            //              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },
            //          new Model { FirstName = "Anna", LastName = "Signorini",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "13616.jpg"},
            //                  new Image { FileName = "12330.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          }, 
            //          new Model { FirstName = "Antonella", LastName = "Signorini",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "13616.jpg"},
            //                  new Image { FileName = "12330.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },
            //          new Model { FirstName = "Antonella", LastName = "Young",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },new Model { FirstName = "Martina", LastName = "Astro",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },new Model { FirstName = "Fidel", LastName = "Astro",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },new Model { FirstName = "Michel", LastName = "Martini",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },new Model { FirstName = "Pamela", LastName = "Young",
            //                              Multimedia = new Multimedia[] {
            //                  new Image { FileName = "12330.jpg"},
            //                  new Image { FileName = "13616.jpg"},
            //                  new Video { FileName = "test.wmv"},
            //              },
            //          },
            //      };
            return response;
        }

        private static Model GetModel(ObjectModel.Person m)
        {
            return new Model
            {
                ID = m.ID,
                FirstName = m.FirstName,
                LastName = m.LastName,
                HairColor = m.PhysicalProfile.HairColor,
                EyeColor = m.PhysicalProfile.EyesColor,
                ResidenceCity = m.ResidenceCity,
                Multimedia = (from mul in m.Multimedia
                              select (mul is ObjectModel.Video) ? (Multimedia)new Video { ID = mul.MultimediaID, FileName = mul.FileName } : new Image { ID = mul.MultimediaID, FileName = mul.FileName }).ToArray()
            };
        }

        #endregion

        #region IModelsManager sample

        //public GetAllModelsResponse GetAllModelsOld(GetAllModelsRequest request)
        //{
        //    GetAllModelsResponse response = new GetAllModelsResponse();
        //    response.Models = new Model[]
        //    {
        //        new Model { FirstName = "Anna", LastName = "Magnani",
        //            Multimedia = new Multimedia[] {
        //                new Image { FileName = "12330.jpg"},
        //                new Image { FileName = "13616.jpg"},
        //                new Video { FileName = "test.wmv"},
        //            },
        //        },
        //        new Model { FirstName = "Anna", LastName = "Signorini",
        //                            Multimedia = new Multimedia[] {
        //                new Image { FileName = "13616.jpg"},
        //                new Image { FileName = "12330.jpg"},
        //                new Video { FileName = "test.wmv"},
        //            },
        //        }, 
        //        new Model { FirstName = "Antonella", LastName = "Signorini",
        //                            Multimedia = new Multimedia[] {
        //                new Image { FileName = "13616.jpg"},
        //                new Image { FileName = "12330.jpg"},
        //                new Video { FileName = "test.wmv"},
        //            },
        //        },
        //        new Model { FirstName = "Antonella", LastName = "Young",
        //                            Multimedia = new Multimedia[] {
        //                new Image { FileName = "12330.jpg"},
        //                new Image { FileName = "13616.jpg"},
        //                new Video { FileName = "test.wmv"},
        //            },
        //        },
        //    };

        //    return response;
        //}

        #endregion
    }
}
